<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateOrdersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('orders', function (Blueprint $table) {
            $table->increments('id');
            //本地订单号
            $table->string('out_trade_no')->unique();
            //微信订单号
            $table->string('transaction_id')->nullable();
            //订单名
            $table->text('body')->nullable();
            //订单详情
            $table->text('detail')->nullable();
            //订单总价
            $table->decimal('total_fee');
            //订单付款时间
            $table->dateTime('paid_at')->nullable();
            //订单所用折扣
            $table->unsignedInteger('coupon_id')->nullable();
            $table->foreign('coupon_id')->references('id')->on('coupons')->onDelete('set null');
            //商品快照
            $table->text('product');
            $table->unsignedInteger('product_id')->nullable();;
            $table->foreign('product_id')->references('id')->on('products')->onDelete('set null');
            //订单用户
            $table->unsignedInteger('user_id');
            $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
            //订单状态
            $table->string('status')->default('processing');
            //退款状态
            $table->string('refund_status');
            //退款单号
            $table->string('refund_no')->nullable();
            //订单是否关闭
            $table->boolean('closed')->default(false);
            //额外信息
            $table->text('extra')->nullable();
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('orders');
    }
}
